﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using System.Text;
using System.Data;

public partial class top_crm_cusapi : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string sql = string.Empty;
        string str = string.Empty;
        string buynick = utils.NewRequest("buynick", utils.RequestType.QueryString);
        string act = utils.NewRequest("act", utils.RequestType.QueryString);
        string mobile = utils.NewRequest("mobile", utils.RequestType.QueryString);
        string truename = utils.NewRequest("truename", utils.RequestType.QueryString);

        if (buynick.Length > 1)
        {
            if (buynick.IndexOf("|") != -1)
            {
                string[] ary = buynick.Split('|');
                for (int i = 0; i < ary.Length; i++) 
                {
                    if (i == 0)
                    {
                        str = GetBuynickInfo(ary[i], act, mobile, truename);
                    }
                    else
                    {
                        str += "," + GetBuynickInfo(ary[i], act, mobile, truename);
                    }
                }
            }
            else
            {
                str = GetBuynickInfo(buynick, act, mobile, truename);
            }
        }
        else
        {
            str = "nobuynick";
        }

        Response.Write(str);
        Response.End();
    }

    private string GetBuynickInfo(string buynick, string act, string mobile, string truename)
    {
        string sql = string.Empty;
        string str = string.Empty;

        string tableName = string.Empty;
        string first = buynick.Substring(0, 1);
        if (IsChineseLetter(buynick, 1))
        {
            tableName = "TCS_CustomerC" + GetChineseSpell(first).ToUpper();
        }
        else
        {
            tableName = "TCS_Customer" + first.ToUpper();
        }

        try
        {
            switch (act)
            {
                case "insert":
                    sql = "[InsertInfoByNick] '" + buynick + "','" + mobile + "','" + truename + "','" + tableName + "'";
                    utils.ExecuteNonQuery(sql);
                    str = "insertok";
                    break;
                case "update":
                    sql = "[UpdateInfoByNick] '" + buynick + "','" + mobile + "','" + truename + "','" + tableName + "'";
                    utils.ExecuteNonQuery(sql);
                    str = "updateok";
                    break;
                case "select":
                    sql = "[GetInfoByNick] '" + buynick + "','" + tableName + "'";
                    DataTable dt = utils.ExecuteDataTable(sql);
                    if (dt.Rows.Count != 0)
                    {
                        str = dt.Rows[0][0].ToString() + "|" + dt.Rows[0][1].ToString();
                    }
                    else
                    {
                        str = "nodata";
                    }
                    break;
                default:
                    str = "noact";
                    break;
            }
        }
        catch { str = "nodata"; }

        return str;
    }

    #region GetChineseSpell获取汉字拼音的第一个字母


    protected bool IsChineseLetter(string input, int index)
    {
        string list = "0123456789abcdefghijklmnopqrstuvwxyz";
        if (list.IndexOf(input.Substring(0,1)) != -1)
        {
            return false;
        }
        else
        {
            return true;
        }

        //int code = 0;
        //int chfrom = Convert.ToInt32("4e00", 16);    //范围（0x4e00～0x9fff）转换成int（chfrom～chend）
        //int chend = Convert.ToInt32("9fff", 16);
        //if (input != "")
        //{
        //    code = Char.ConvertToUtf32(input, index);    //获得字符串input中指定索引index处字符unicode编码
        //    if (code >= chfrom && code <= chend)
        //    {
        //        return true;     //当code在中文范围内返回true
        //    }
        //    else
        //    {
        //        return false;    //当code不在中文范围内返回false
        //    }
        //}
        //return false;
    }


    //获取汉字拼音的第一个字母
    static public string GetChineseSpell(string strText)
    {
        int len = strText.Length;
        string myStr = "";
        for (int i = 0; i < len; i++)
        {
            myStr += getSpell(strText.Substring(i, 1));
        }
        return myStr;
    }

    static public string[] GetChineseSpell(string[] strText)
    {
        int len = strText.Length;
        string[] myStr = null;
        for (int i = 0; i < len; i++)
        {
            myStr[i] = getSpell(strText[i]);
        }
        return myStr;
    }

    static public string getSpell(string cnChar)
    {
        byte[] arrCN = Encoding.Default.GetBytes(cnChar);
        if (arrCN.Length > 1)
        {
            int area = (short)arrCN[0];
            int pos = (short)arrCN[1];
            int code = (area << 8) + pos;
            int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };
            for (int i = 0; i < 26; i++)
            {
                int max = 55290;
                if (i != 25) max = areacode[i + 1];
                if (areacode[i] <= code && code < max)
                {
                    return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
                }
            }
            return "0";
        }
        else return cnChar;
    }
    #endregion
}